<?php

namespace app\api\model;

use app\common\model\Lesson as LessonModel;

/**
 * 商品分类模型
 * Class Category
 * @package app\common\model
 */
class Lesson extends LessonModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'wxapp_id',
//        'create_time',
        'update_time'
    ];

    public  function content($post) {
        $category_id = $post['category_id'];
        if (empty($category_id)){
            $this->error = "内容编号为空";
            return false;
        }
        $contents =  $this->with(['category','details'])
            ->where('category_id','=',$category_id)
            ->where('is_delete','=',0)
            ->find();
        if (empty($contents)){
            $this->error = "内容错误";
            return false;
        }
        //整理内容
        $tem_arr = $contents->toArray();
        $cata = $tem_arr['category'];
        $contentlist = $tem_arr['details'];
        //资源文件列表
        $resources = [];
        $content['lesson_id'] = $tem_arr['lesson_id'];
        $content['category_id'] = $tem_arr['category_id'];
        $content['outline'] = html_entity_decode($tem_arr['outline']);
        $content['knowledge'] = html_entity_decode($tem_arr['knowledge']);
        $content['reviewpoints'] = html_entity_decode($tem_arr['reviewpoints']);
        $content['category'] = $cata['name'];

        if (count($contentlist)<1){
            $this->error = "本节课还未上传任何课件内容";
            return false;
        }
        $tem_content = [];
        foreach ($contentlist as $k=>$v){
            $details_content = $v['type']=='article'?html_entity_decode($v['content']):json_decode($v['content'],true);
            $tem_content[$k]['type'] = $v['type'];
            $tem_content[$k]['content'] = $details_content;
            $tem_content[$k]['name'] = $v['name'];
            $tem_content[$k]['sort'] = $v['sort'];
            $this->getresources($resources,$v['type'],$details_content);
        }
        $content['resources'] = $resources;
        //将内容进行排序
        $last_sort = array_column($tem_content,'sort');
        array_multisort($last_sort,SORT_ASC,$tem_content);
        $content['list'] = $tem_content;
        return $content;
    }

    /**
     * 获取资源
     */
    private function getresources(&$tem,$type,$data){
        switch ($type){
            case 'silde':
                foreach ($data as $k=>$v){
                    $tem[] = ['src'=>$v['src'],'id'=>$v['id']];
                    if (!empty($v['audiosrc'])){
                        $tem[] = ['src'=>$v['audiosrc'],'id'=>$v['audioid']];
                    }
                }
                break;
            case 'video':
                foreach ($data as $k=>$v){
                    $tem[] = ['src'=>$v['src'],'id'=>$v['id']];
                }
                break;
            case 'audio':
                    $tem[] = ['src'=>$data['src'],'id'=>$data['id']];
                    if (count($data['images'])>0){
                        foreach ($data['images'] as $k=>$v){
                            $tem[] = ['src'=>$v['src'],'id'=>$v['id']];
                        }
                    }
                break;
            case 'article':
                break;
        }
    }

}
